package com.zy.configs;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * @program: shopping153_cloud
 * @description:
 * @author: zy
 * @create: 2025-08-22 10:51
 */
//@Configuration
public class RandomLoadBalancerConfig {

    //创建  RandomLoadBalancer 负载均衡器
    @Bean
    public ReactorLoadBalancer<ServiceInstance> rlb(Environment environment,
                                                    LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        // 上面代码 :   environment.getProperty("loadbalancer.client.name") -> hello

        return new RandomLoadBalancer(  loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)  , name);
        // return new MyOnlyOneLoadBalancer(  loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class)  , name   );
    }
}
